package org.csu.onlineedubackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.onlineedubackend.entity.FileRecord;
import org.csu.onlineedubackend.mapper.FileRecordMapper;
import org.csu.onlineedubackend.service.FileRecordService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FileRecordServiceImpl extends ServiceImpl<FileRecordMapper, FileRecord> implements FileRecordService {

    @Override
    public List<FileRecord> getFilesByUser(Integer userId) {
        return list(new QueryWrapper<FileRecord>().eq("user_id", userId));
    }
    @Override
    public boolean save(FileRecord record) {
        return this.saveOrUpdate(record); // 你也可以用 save(record) 也行
    }
    @Override
    public boolean deleteById(Integer id) {
        return this.removeById(id);
    }
    @Override
    public FileRecord getById(Integer id) {
        return this.getBaseMapper().selectById(id);
    }
    @Override
    public List<FileRecord> listAll() {
        return this.list();
    }
    
    @Override
    public FileRecord getFileByFileName(String fileName) {
        // 从文件URL中提取文件名进行查询
        QueryWrapper<FileRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("file_url", fileName);
        return this.getOne(queryWrapper);
    }
}
